setwd("___YOUR FILE PATH HERE___") 


library(foreign)
library(interplot)
library(ltm)
library(stargazer)
library(tidyverse)
library(lmtest)
library(multiwayvcov)
library(plm)
library(gridExtra)
library(emmeans)
library(broom)
library(dotwhisker)
require(lattice)
library(reshape2)
library(sjPlot)
library(effects)
library(ape)
library(dplyr)

devtools::install_github('gibbonscharlie/bfe')
library(bfe)


#### PREP DATA

evs<-read.csv('EVS_main.csv', stringsAsFactors = F)


## mean centering covariates for the multilevel analysis
evs$gpc.Distance <- evs$Distance - mean(evs$Distance)
evs$gpc.prop_jewish25 <- evs$prop_jewish25 - mean(evs$prop_jewish25)
evs$gpc.unemployment33 <- evs$unemployment33 - mean(evs$unemployment33)
evs$gpc.population25 <- evs$population25 - mean(evs$population25)
evs$gpc.nazishare33 <- evs$nazishare33 - mean(evs$nazishare33)


## generating group-level variables
evs <- evs %>% 
  group_by(state) %>% 
  mutate(MeanDistanceS = mean(gpc.Distance), MeanJewsS = mean(gpc.prop_jewish25), MeanNazisS = mean(gpc.nazishare33))
evs <- evs %>% ungroup()

evs <- evs %>% 
  group_by(closest_camp) %>% 
  mutate(MeanDistanceC = mean(gpc.Distance), MeanJewsC = mean(gpc.prop_jewish25), MeanNazisC = mean(gpc.nazishare33))
evs <- evs %>% ungroup()




## naming the fixed effects
evs$f.state <- as.factor(evs$state)
evs$state <- recode(evs$state, DE1 = "WEST:\nBaden-Wurttemberg",
                           DE2 = "WEST:\nBavaria", 
                           DE3 = "EAST:\nBerlin",
                           DE4 = "EAST:\nBrandenburg",
                           DE5 = "WEST:\nBremen",
                           DE6 = "WEST:\nHamburg",
                           DE7 = "WEST:\nHessen",
                           DE8 = "EAST:\nMecklenburg-Vorpommern",
                           DE9 = "WEST:\nLower Saxony",
                           DEA = "WEST:\nNorth Rhine-Westphalia",
                           DEB = "WEST:\nRhineland Palatinate",
                           DEC = "WEST:\nSaarland",
                           DED = "EAST:\nSaxony",
                           DEE = "EAST:\nSaxony-Anhalt",
                           DEF = "WEST:\nSchleswig-Holstein",
                           DEG = "EAST:\nThuringia")

## creating distance matrices
state.f = factor(evs$state)
state.dummies = model.matrix(~state.f+0)
state.dist <- state.dummies%*%t(state.dummies)
camp.f = factor(evs$closest_camp)
camp.dummies = model.matrix(~camp.f+0)
camp.dist <- camp.dummies%*%t(camp.dummies)


### Moran's Tests for Text (pp. 14-15)
Moran.I(evs$Distance, state.dist)
Moran.I(evs$intolerance, state.dist)
Moran.I(evs$resentment, state.dist)
Moran.I(evs$far_right, state.dist)


####### FIGURE 1

p1 <- ggplot(evs, aes(x=Distance, y=intolerance)) + geom_point(shape = 21, size=1) + geom_smooth(method=lm, se=FALSE) + theme_classic()
p2 <- ggplot(evs, aes(x=Distance, y=intolerance)) + geom_point(shape = 21, size=1) + geom_smooth(method=lm, se=FALSE) + theme_bw() +
  facet_wrap(vars(state)) +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black"))

p3 <- ggplot(evs, aes(x=Distance, y=resentment)) + geom_point(shape = 21, size=1) + geom_smooth(method=lm, se=FALSE) + theme_classic()
p4 <- ggplot(evs, aes(x=Distance, y=resentment)) + geom_point(shape = 21, size=1) + geom_smooth(method=lm, se=FALSE) + theme_bw() +
  facet_wrap(vars(state)) +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black"))

p5 <- ggplot(evs, aes(x=Distance, y=far_right)) + geom_point(shape = 21, size=1) + geom_smooth(method=lm, se=FALSE) + theme_classic()
p6 <- ggplot(evs, aes(x=Distance, y=far_right)) + geom_point(shape = 21, size=1) + geom_smooth(method=lm, se=FALSE) + theme_bw() +
  facet_wrap(vars(state)) +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black"))

lay <- rbind(c(1,2,2),
             c(1,2,2),
             c(1,2,2),
             c(3,4,4),
             c(3,4,4),
             c(3,4,4),
             c(5,6,6),
             c(5,6,6),
             c(5,6,6))

jpeg("figure1.jpg", height=900, width=1000)
grid.arrange(p1, p2, p3, p4, p5, p6, layout_matrix = lay)
dev.off()



################# Table 1

# Outcome: Intolerance toward outgroups

m1b <- lm(intolerance~Distance,evs)
m1bfe <- lm(intolerance~Distance + I(state),evs)

#With Pre-treatment variables
m1<-lm(intolerance~Distance+prop_jewish25+unemployment33+population25+nazishare33 ,evs)
m1fe<-lm(intolerance~Distance+prop_jewish25+unemployment33+population25+nazishare33+I(state) ,evs)

#With Pre/Post-treatment variables
m1f<-lm(intolerance~
          Distance+
          prop_jewish25+
          unemployment33+
          population25+
          nazishare33+
          #Post-treatment
          lr+
          immigrants07+
          unemployment07+
          unemp+
          educ+
          female+
          age+
          urban_scale+
          west,evs)

#With Pre/Post-treatment variables
m1ffe<-lm(intolerance~
          Distance+
          prop_jewish25+
          unemployment33+
          population25+
          nazishare33+
          #Post-treatment
          lr+
          immigrants07+
          unemployment07+
          unemp+
          educ+
          female+
          age+
          urban_scale+
          west+I(state) ,evs)

cl.cov <- cluster.vcov(m1ffe, evs$state) # cluster-robust SEs for m1ffe
cl.robust.se <- sqrt(diag(cl.cov))


#G-estimator 1st stage
m1s<-lm(I(intolerance - 
            coef(m1ffe)['immigrants07']*immigrants07 -
            coef(m1ffe)['lr']*lr -
            coef(m1ffe)['unemp']*unemp - 
            coef(m1ffe)['unemployment07']*unemployment07 - 
            coef(m1ffe)['educ']*educ -
            coef(m1ffe)['urban_scale']*urban_scale) ~ 
          Distance+
          prop_jewish25+
          unemployment33+
          population25+
          nazishare33+I(state), evs)

#Full G-estimator
boots <- 1000
set.seed(543)
fl.boots <- matrix(NA, nrow=boots, ncol=6)
for(b in 1:boots){
  d.star <- evs[sample(1:nrow(evs), replace=TRUE),]
  #G-estimator 1st stage
  boot.first <- lm(intolerance~
                     Distance+
                     prop_jewish25+
                     unemployment33+
                     population25+
                     nazishare33+
                     #Post-treatment
                     lr+ 
                     immigrants07+
                     unemployment07+
                     unemp+
                     educ+
                     female+
                     age+
                     urban_scale+
                     west+I(state),
                   d.star)
  #G-estimator 2nd stage
  boot.direct <- lm(I(intolerance - 
                        coef(boot.first)['immigrants07']*immigrants07 -
                        coef(boot.first)['lr']*lr -
                        coef(boot.first)['unemp']*unemp - 
                        coef(boot.first)['unemployment07']*unemployment07 - 
                        coef(boot.first)['educ']*educ -
                        coef(boot.first)['urban_scale']*urban_scale) ~ 
                      Distance+
                      prop_jewish25+
                      unemployment33+
                      population25+
                      nazishare33,
                    d.star)
  fl.boots[b,] <- coef(boot.direct)
}
#Extracting bootstrapped SEs
SEs1 <- apply(fl.boots,2,sd)

fes <- c()

stargazer(m1b,m1bfe,m1,m1fe,m1f,m1ffe,m1ffe,m1s,se=list(NULL,NULL,NULL,NULL,NULL,NULL,cl.robust.se,SEs1),dep.var.labels.include = FALSE,type="text",
          out="table1_panela.txt", no.space=T, 
          keep = c("Distance","prop_jewish25","unemployment33","population25","nazishare33"),
          star.cutoffs = c(0.05, 0.01, NA), keep.stat = c("n","adj.rsq"),
          add.lines=list(c("Land Fixed Effects","No","Yes","No","Yes","No","Yes","Yes","Yes"),
                         c("Post-Treatment Controls","No","No","No","No","Yes","Yes","Yes","Yes"),
                         c("Clustered SEs","No","No","No","No","No","No","Yes","No"),
                         c("Method","OLS","OLS","OLS","OLS","OLS","OLS","OLS","G-est")), 
          covariate.labels = c('Distance to camp', '\\% Jews (1925)', '\\% Unemployed (1933)',
                               'Population (1925)', 'Nazi party share (1933)'))



# Outcome: Immigrant Resentment
m2b <- lm(resentment~Distance,evs)
m2bfe <- lm(resentment~Distance + I(state),evs)

#With Pre-treatment variables
m2<-lm(resentment~Distance+prop_jewish25+unemployment33+population25+nazishare33 ,evs)
m2fe<-lm(resentment~Distance+prop_jewish25+unemployment33+population25+nazishare33+I(state) ,evs)

#With Pre/Post-treatment variables
m2f<-lm(resentment~Distance+
          prop_jewish25+
          unemployment33+
          population25+
          nazishare33+
          #Post-treatment
          lr+
          immigrants07+
          unemployment07+
          unemp+
          educ+
          female+
          age+
          urban_scale+
          west,evs)

#With Pre/Post-treatment variables
m2ffe<-lm(resentment~Distance+
          prop_jewish25+
          unemployment33+
          population25+
          nazishare33+
          #Post-treatment
          lr+
          immigrants07+
          unemployment07+
          unemp+
          educ+
          female+
          age+
          urban_scale+
          west+I(state) ,evs)

cl.cov <- cluster.vcov(m2ffe, evs$state) # cluster-robust SEs for m1ffe
cl.robust.se <- sqrt(diag(cl.cov))


#G-estimator 1st stage
m2s<-lm(I(resentment - 
            coef(m2ffe)['immigrants07']*immigrants07 -
            coef(m2ffe)['lr']*lr -
            coef(m2ffe)['unemp']*unemp - 
            coef(m2ffe)['unemployment07']*unemployment07 - 
            coef(m2ffe)['educ']*educ -
            coef(m2ffe)['urban_scale']*urban_scale) ~ 
          Distance+
          prop_jewish25+
          unemployment33+
          population25+
          nazishare33+I(state), evs)

#Full G-estimator
boots <- 1000
set.seed(543)
fl.boots <- matrix(NA, nrow=boots, ncol=6)
for(b in 1:boots){
  d.star <- evs[sample(1:nrow(evs), replace=TRUE),]
  boot.first <- lm(resentment~Distance+
                     prop_jewish25+
                     unemployment33+
                     population25+
                     nazishare33+
                     #Post-treatment
                     lr+
                     immigrants07+
                     unemployment07+
                     unemp+
                     educ+
                     female+
                     age+
                     urban_scale+
                     west+I(state),d.star)
  boot.direct <- lm(I(resentment - 
                        coef(boot.first)['immigrants07']*immigrants07 -
                        coef(boot.first)['lr']*lr -
                        coef(boot.first)['unemp']*unemp - 
                        coef(boot.first)['unemployment07']*unemployment07 - 
                        coef(boot.first)['educ']*educ -
                        coef(boot.first)['urban_scale']*urban_scale) ~ 
                      Distance+
                      prop_jewish25+
                      unemployment33+
                      population25+
                      nazishare33, d.star)
  fl.boots[b,] <- coef(boot.direct)
  
  
}
#Extracting bootstrapped SEs
SEs2 <- apply(fl.boots,2,sd)

stargazer(m2b,m2bfe,m2,m2fe,m2f,m2ffe,m2ffe,m2s,se=list(NULL,NULL,NULL,NULL,NULL,NULL,cl.robust.se,SEs2),dep.var.labels.include = FALSE,type="text",
          out="table1_panelb.txt", no.space=T, 
          keep = c("Distance","prop_jewish25","unemployment33","population25","nazishare33"),
          star.cutoffs = c(0.05, 0.01, NA), keep.stat = c("n","adj.rsq"),
          add.lines=list(c("Land Fixed Effects","No","Yes","No","Yes","No","Yes","Yes","Yes"),
                         c("Post-Treatment Controls","No","No","No","No","Yes","Yes","Yes","Yes"),
                         c("Clustered SEs","No","No","No","No","No","No","Yes","No"),
                         c("Method","OLS","OLS","OLS","OLS","OLS","OLS","OLS","G-est")), 
          covariate.labels = c('Distance to camp', '\\% Jews (1925)', '\\% Unemployed (1933)',
                               'Population (1925)', 'Nazi party share (1933)'))


# Outcome: Support for Extreme Right-Wing Parties
m3b <- lm(far_right~Distance,evs)
m3bfe <- lm(far_right~Distance + I(state),evs)

#With Pre-treatment variables
m3<-lm(far_right~Distance+prop_jewish25+unemployment33+population25+nazishare33 ,evs)
m3fe<-lm(far_right~Distance+prop_jewish25+unemployment33+population25+nazishare33+I(state) ,evs)

#With Pre/Post-treatment variables
m3f<-lm(far_right~Distance+
          prop_jewish25+
          unemployment33+
          population25+
          nazishare33+
          #Post-treatment
          lr+ 
          immigrants07+
          unemployment07+
          unemp+
          educ+
          female+
          age+
          urban_scale+
          west ,evs)

m3ffe<-lm(far_right~Distance+
          prop_jewish25+
          unemployment33+
          population25+
          nazishare33+
          #Post-treatment
          lr+ 
          immigrants07+
          unemployment07+
          unemp+
          educ+
          female+
          age+
          urban_scale+
          west+I(state) ,evs)

cl.cov <- cluster.vcov(m3ffe, evs$state) # cluster-robust SEs for m1ffe
cl.robust.se <- sqrt(diag(cl.cov))


#G-estimator 1st stage
m3s<-lm(I(far_right - 
            coef(m3ffe)['immigrants07']*immigrants07 -
            coef(m3ffe)['lr']*lr -
            coef(m3ffe)['unemp']*unemp - 
            coef(m3ffe)['unemployment07']*unemployment07 - 
            coef(m3ffe)['educ']*educ -
            coef(m3ffe)['urban_scale']*urban_scale) ~ 
          Distance+
          prop_jewish25+
          unemployment33+
          population25+
          nazishare33+I(state), evs)

#Full G-estimator
boots <- 1000
set.seed(543)
fl.boots <- matrix(NA, nrow=boots, ncol=6)
for(b in 1:boots){
  d.star <- evs[sample(1:nrow(evs), replace=TRUE),]
  boot.first <- lm(far_right~Distance+
                     prop_jewish25+
                     unemployment33+
                     population25+
                     nazishare33+
                     #Post-treatment
                     lr+ 
                     immigrants07+
                     unemployment07+
                     unemp+
                     educ+
                     female+
                     age+
                     urban_scale+
                     west+I(state),d.star)
  boot.direct <- lm(I(far_right - 
                        coef(boot.first)['immigrants07']*immigrants07 -
                        coef(boot.first)['lr']*lr -
                        coef(boot.first)['unemp']*unemp - 
                        coef(boot.first)['unemployment07']*unemployment07 - 
                        coef(boot.first)['educ']*educ -
                        coef(boot.first)['urban_scale']*urban_scale) ~ 
                      Distance+
                      prop_jewish25+
                      unemployment33+
                      population25+
                      nazishare33, d.star)
  fl.boots[b,] <- coef(boot.direct)
}
#Extracting bootstrapped SEs
SEs3 <- apply(fl.boots,2,sd)

stargazer(m3b,m3bfe,m3,m3fe,m3f,m3ffe,m3ffe,m3s,se=list(NULL,NULL,NULL,NULL,NULL,NULL,cl.robust.se,SEs3),dep.var.labels.include = FALSE,type="text",
          out="table1_panelc.txt", no.space=T, 
          keep = c("Distance","prop_jewish25","unemployment33","population25","nazishare33"),
          star.cutoffs = c(0.05, 0.01, NA),  keep.stat = c("n","adj.rsq"),
          add.lines=list(c("Land Fixed Effects","No","Yes","No","Yes","No","Yes","Yes","Yes"),
                         c("Post-Treatment Controls","No","No","No","No","Yes","Yes","Yes","Yes"),
                         c("Clustered SEs","No","No","No","No","No","No","Yes","No"),
                         c("Method","OLS","OLS","OLS","OLS","OLS","OLS","OLS","G-est")), 
          covariate.labels = c('Distance to camp', '\\% Jews (1925)', '\\% Unemployed (1933)',
                               'Population (1925)', 'Nazi party share (1933)'))


##### FOOTNOTE: MORAN'S I STATISTICS

resid.m1 <- residuals(m1)
Moran.I(resid.m1, state.dist)
resid.m2 <- residuals(m2)
Moran.I(resid.m2, state.dist)
resid.m3 <- residuals(m3)
Moran.I(resid.m3, state.dist)


####### TABLE 2:  HAUSMAN Tests

# bivariate models
form <- intolerance~Distance
wi1 <- plm(form, data = evs, model = "within", index="state")
re1 <- plm(form, data = evs, model = "random", index="state")
pooled1 <- plm(form, data = evs, model = "pooling", index="state")
re.v.pooled1 <- round(phtest(re1, pooled1)$p.value, digits=3)
fe.v.pooled1 <- round(phtest(wi1, pooled1)$p.value, digits=3)
fe.v.re1 <- round(phtest(wi1, re1)$p.value, digits=3)

form <- resentment~Distance
wi2 <- plm(form, data = evs, model = "within", index="state")
re2 <- plm(form, data = evs, model = "random", index="state")
pooled2 <- plm(form, data = evs, model = "pooling", index="state")
re.v.pooled2 <- round(phtest(re2, pooled2)$p.value, digits=3)
fe.v.pooled2 <- round(phtest(wi2, pooled2)$p.value, digits=3)
fe.v.re2 <- round(phtest(wi2, re2)$p.value, digits=3)

form <- far_right~Distance
wi3 <- plm(form, data = evs, model = "within", index="state")
re3 <- plm(form, data = evs, model = "random", index="state")
pooled3 <- plm(form, data = evs, model = "pooling", index="state")
re.v.pooled3 <- round(phtest(re3, pooled3)$p.value, digits=3)
fe.v.pooled3 <- round(phtest(wi3, pooled3)$p.value, digits=3)
fe.v.re3 <- round(phtest(wi3, re3)$p.value, digits=3)

stargazer(pooled1,re1,wi1, pooled2,re2,wi2, pooled3,re3,wi3, 
          out="table2_panela.txt", no.space=T, keep="Distance",
          star.cutoffs = c(0.05, 0.01, NA), keep.stat = c("n","adj.rsq"),
          add.lines=list(c("Method","Pooled","RE","FE","Pooled","RE","FE","Pooled","RE","FE"),
                         c("RE v Pooled",re.v.pooled1,"","",re.v.pooled2,"","",re.v.pooled3,"",""),
                         c("FE v Pooled",fe.v.pooled1,"","",fe.v.pooled2,"","",fe.v.pooled3,"",""),
                         c("FE v RE",fe.v.re1,"","",fe.v.re2,"","",fe.v.re3,"","")))

# prewar only models
form <- intolerance~Distance+prop_jewish25+unemployment33+population25+nazishare33
wi1 <- plm(form, data = evs, model = "within", index="state")
re1 <- plm(form, data = evs, model = "random", index="state")
pooled1 <- plm(form, data = evs, model = "pooling", index="state")
re.v.pooled1 <- round(phtest(re1, pooled1)$p.value, digits=3)
fe.v.pooled1 <- round(phtest(wi1, pooled1)$p.value, digits=3)
fe.v.re1 <- round(phtest(wi1, re1)$p.value, digits=3)

form <- resentment~Distance+prop_jewish25+unemployment33+population25+nazishare33
wi2 <- plm(form, data = evs, model = "within", index="state")
re2 <- plm(form, data = evs, model = "random", index="state")
pooled2 <- plm(form, data = evs, model = "pooling", index="state")
re.v.pooled2 <- round(phtest(re2, pooled2)$p.value, digits=3)
fe.v.pooled2 <- round(phtest(wi2, pooled2)$p.value, digits=3)
fe.v.re2 <- round(phtest(wi2, re2)$p.value, digits=3)

form <- far_right~Distance+prop_jewish25+unemployment33+population25+nazishare33
wi3<- plm(form, data = evs, model = "within", index="state")
re3 <- plm(form, data = evs, model = "random", index="state")
pooled3 <- plm(form, data = evs, model = "pooling", index="state")
re.v.pooled3 <- round(phtest(re3, pooled3)$p.value, digits=3)
fe.v.pooled3 <- round(phtest(wi3, pooled3)$p.value, digits=3)
fe.v.re3 <- round(phtest(wi3, re3)$p.value, digits=3)

stargazer(pooled1,re1,wi1, pooled2,re2,wi2, pooled3,re3,wi3, 
          out="table2_panelb.txt", no.space=T, keep="Distance",
          star.cutoffs = c(0.05, 0.01, NA), keep.stat = c("n","adj.rsq"),
          add.lines=list(c("Method","Pooled","RE","FE","Pooled","RE","FE","Pooled","RE","FE"),
                         c("RE v Pooled",re.v.pooled1,"","",re.v.pooled2,"","",re.v.pooled3,"",""),
                         c("FE v Pooled",fe.v.pooled1,"","",fe.v.pooled2,"","",fe.v.pooled3,"",""),
                         c("FE v RE",fe.v.re1,"","",fe.v.re2,"","",fe.v.re3,"","")))

# pre/postwar models
form <- intolerance~Distance+prop_jewish25+unemployment33+population25+nazishare33+
  lr+immigrants07+unemployment07+unemp+educ+female+age+urban_scale+west
wi1 <- plm(form, data = evs, model = "within", index="state")
re1 <- plm(form, data = evs, model = "random", index="state")
pooled1 <- plm(form, data = evs, model = "pooling", index="state")
re.v.pooled1 <- round(phtest(re1, pooled1)$p.value, digits=3)
fe.v.pooled1 <- round(phtest(wi1, pooled1)$p.value, digits=3)
fe.v.re1 <- round(phtest(wi1, re1)$p.value, digits=3)

form <- resentment~Distance+prop_jewish25+unemployment33+population25+nazishare33+
  lr+immigrants07+unemployment07+unemp+educ+female+age+urban_scale+west
wi2 <- plm(form, data = evs, model = "within", index="state")
re2 <- plm(form, data = evs, model = "random", index="state")
pooled2 <- plm(form, data = evs, model = "pooling", index="state")
re.v.pooled2 <- round(phtest(re2, pooled2)$p.value, digits=3)
fe.v.pooled2 <- round(phtest(wi2, pooled2)$p.value, digits=3)
fe.v.re2 <- round(phtest(wi2, re2)$p.value, digits=3)

form <- far_right~Distance+prop_jewish25+unemployment33+population25+nazishare33+
  lr+immigrants07+unemployment07+unemp+educ+female+age+urban_scale+west
wi3 <- plm(form, data = evs, model = "within", index="state")
re3 <- plm(form, data = evs, model = "random", index="state")
pooled3 <- plm(form, data = evs, model = "pooling", index="state")
re.v.pooled3 <- round(phtest(re3, pooled3)$p.value, digits=3)
fe.v.pooled3 <- round(phtest(wi3, pooled3)$p.value, digits=3)
fe.v.re3 <- round(phtest(wi3, re3)$p.value, digits=3)
stargazer(pooled1,re1,wi1, pooled2,re2,wi2, pooled3,re3,wi3, 
          out="table2_panelc.txt", no.space=T, keep="Distance",
          star.cutoffs = c(0.05, 0.01, NA), keep.stat = c("n","adj.rsq"),
          add.lines=list(c("Method","Pooled","RE","FE","Pooled","RE","FE","Pooled","RE","FE"),
                         c("RE v Pooled",re.v.pooled1,"","",re.v.pooled2,"","",re.v.pooled3,"",""),
                         c("FE v Pooled",fe.v.pooled1,"","",fe.v.pooled2,"","",fe.v.pooled3,"",""),
                         c("FE v RE",fe.v.re1,"","",fe.v.re2,"","",fe.v.re3,"","")))



########## TABLE 3: REWEIGHTED ANALYSES

# first prep the data. Have to exclude any state with no variation in Distance
data.evs <- data.frame(evs)
data.evs <- data.evs[data.evs$f.state!="DE3" & data.evs$f.state!="DE6",]

# estimate and save results for intolerance
m1.iwe <- EstimateIWE(y="intolerance", treatment="Distance", group="f.state", 
                      controls=c("prop_jewish25","unemployment33","population25","nazishare33"),
                      data=data.evs, subset=NULL, cluster.var=NULL,is.robust=TRUE)
m1.fe <- rbind(m1.iwe$fe.est, sqrt(m1.iwe$fe.var), m1.iwe$fe.est/sqrt(m1.iwe$fe.var))
m1.iwe <- rbind(m1.iwe$swe.est, sqrt(m1.iwe$swe.var), m1.iwe$swe.est/sqrt(m1.iwe$swe.var))
m1.base <- rbind(summary(m1)$coefficients[2,1],summary(m1)$coefficients[2,2],summary(m1)$coefficients[2,3])

m1.rwe <- EstimateRWE(y="intolerance", treatment="Distance", group="f.state", 
                      controls=c("prop_jewish25","unemployment33","population25","nazishare33"),
                      data=data.evs, subset=NULL, cluster.var=NULL,is.robust=TRUE)
m1.rwe <- rbind(m1.rwe$swe.est, sqrt(m1.rwe$swe.var), m1.rwe$swe.est/sqrt(m1.rwe$swe.var))

res.m1 <- cbind(m1.base, m1.fe, m1.iwe, m1.rwe)
colnames(res.m1) <- c("Pooled","FE","IWE","RWE")
write.table(res.m1, file="table3_panela.txt", row.names=FALSE)

# estimate and save results for resentment
m2.iwe <- EstimateIWE(y="resentment", treatment="Distance", group="f.state", 
                      controls=c("prop_jewish25","unemployment33","population25","nazishare33"),
                      data=data.evs, subset=NULL, cluster.var=NULL,is.robust=TRUE)
m2.fe <- rbind(m2.iwe$fe.est, sqrt(m2.iwe$fe.var), m2.iwe$fe.est/sqrt(m2.iwe$fe.var))
m2.iwe <- rbind(m2.iwe$swe.est, sqrt(m2.iwe$swe.var), m2.iwe$swe.est/sqrt(m2.iwe$swe.var))
m2.base <- rbind(summary(m2)$coefficients[2,1],summary(m2)$coefficients[2,2],summary(m2)$coefficients[2,3])

m2.rwe <- EstimateRWE(y="resentment", treatment="Distance", group="f.state", 
                      controls=c("prop_jewish25","unemployment33","population25","nazishare33"),
                      data=data.evs, subset=NULL, cluster.var=NULL,is.robust=TRUE)
m2.rwe <- rbind(m2.rwe$swe.est, sqrt(m2.rwe$swe.var), m2.rwe$swe.est/sqrt(m2.rwe$swe.var))

res.m2 <- cbind(m2.base, m2.fe, m2.iwe, m2.rwe)
colnames(res.m2) <- c("Pooled","FE","IWE","RWE")
write.table(res.m2, file="table3_panelb.txt", row.names=FALSE)

# estimate and save results for far-right support
m3.iwe <- EstimateIWE(y="far_right", treatment="Distance", group="f.state", 
                      controls=c("prop_jewish25","unemployment33","population25","nazishare33"),
                      data=data.evs, subset=NULL, cluster.var=NULL,is.robust=TRUE)
m3.fe <- rbind(m3.iwe$fe.est, sqrt(m3.iwe$fe.var), m3.iwe$fe.est/sqrt(m3.iwe$fe.var))
m3.iwe <- rbind(m3.iwe$swe.est, sqrt(m3.iwe$swe.var), m3.iwe$swe.est/sqrt(m3.iwe$swe.var))
m3.base <- rbind(summary(m3)$coefficients[2,1],summary(m3)$coefficients[2,2],summary(m3)$coefficients[2,3])

m3.rwe <- EstimateRWE(y="far_right", treatment="Distance", group="f.state", 
                      controls=c("prop_jewish25","unemployment33","population25","nazishare33"),
                      data=data.evs, subset=NULL, cluster.var=NULL,is.robust=TRUE)
m3.rwe <- rbind(m3.rwe$swe.est, sqrt(m3.rwe$swe.var), m3.rwe$swe.est/sqrt(m3.rwe$swe.var))

res.m3 <- cbind(m3.base, m3.fe, m3.iwe, m3.rwe)
colnames(res.m3) <- c("Pooled","FE","IWE","RWE")
write.table(res.m3, file="table3_panelc.txt", row.names=FALSE)


######## ADDITIONAL MORAN'S I TESTS FOR FOOTNOTE 3

m1<-lm(intolerance~Distance+prop_jewish25+unemployment33+population25+nazishare33 + I(closest_camp),evs)
resid.m1 <- residuals(m1)
Moran.I(resid.m1, state.dist)
m1<-lmer(intolerance~Distance+prop_jewish25+unemployment33+population25+nazishare33 + (1 | closest_camp),evs)
resid.m1 <- residuals(m1)
Moran.I(resid.m1, state.dist)

m2<-lm(resentment~Distance+prop_jewish25+unemployment33+population25+nazishare33 + I(closest_camp),evs)
resid.m2 <- residuals(m2)
Moran.I(resid.m2, state.dist)
m2<-lmer(resentment~Distance+prop_jewish25+unemployment33+population25+nazishare33 + (1 | closest_camp),evs)
resid.m1 <- residuals(m1)
Moran.I(resid.m1, state.dist)

m3<-lm(far_right~Distance+prop_jewish25+unemployment33+population25+nazishare33 + I(closest_camp),evs)
resid.m3 <- residuals(m3)
Moran.I(resid.m3, state.dist)
m3<-lmer(far_right~Distance+prop_jewish25+unemployment33+population25+nazishare33 + (1 | closest_camp),evs)
resid.m3 <- residuals(m3)
Moran.I(resid.m3, state.dist)




####### TABLE 4: Random Slopes and Random Coefficients

# DV: INTOLERANCE

ri.state.i <- lmer(intolerance~gpc.Distance + gpc.prop_jewish25+gpc.unemployment33+gpc.population25+gpc.nazishare33 + 
                     ( 1 | state) ,evs,
                   control = lmerControl(optimizer ="Nelder_Mead"))
ri.state.camp.i <- lmer(intolerance~gpc.Distance + gpc.prop_jewish25+gpc.unemployment33+gpc.population25+gpc.nazishare33 + 
                          ( 1 | state) + (1 | closest_camp),evs,
                        control = lmerControl(optimizer ="Nelder_Mead"))
rs.state.camp.i <- lmer(intolerance~gpc.Distance + gpc.prop_jewish25+gpc.unemployment33+gpc.population25+gpc.nazishare33 + 
                          (1 + gpc.Distance| state) + (1 + gpc.Distance| closest_camp),evs,
                        control = lmerControl(optimizer ="Nelder_Mead"))
urs.state.camp.i <- lmer(intolerance~gpc.Distance + gpc.prop_jewish25+gpc.unemployment33+gpc.population25+gpc.nazishare33 + 
                           (1 | state) + (0 + gpc.Distance | state) + 
                           (1 | closest_camp) + (0 + gpc.Distance | closest_camp),
                         evs, control = lmerControl(optimizer ="Nelder_Mead"))
anova(ri.state.i,ri.state.camp.i,rs.state.camp.i,urs.state.camp.i)
anova(ri.state.i,ri.state.camp.i,rs.state.camp.i)
tab_model(ri.state.i,ri.state.camp.i,urs.state.camp.i,rs.state.camp.i, digits=3,
          show.se=TRUE, show.ci=FALSE, collapse.se=TRUE, show.p=FALSE, p.style="asterisk", p.threshold = c(0.05, 0.01))
stargazer(ri.state.i,ri.state.camp.i,urs.state.camp.i,rs.state.camp.i,dep.var.labels.include = FALSE,type="text",
          out="table4_panela.txt", no.space=T, 
          keep = c("Distance"),
          star.cutoffs = c(0.05, 0.01, NA),  keep.stat = c("n","adj.rsq"),
          add.lines=list(c("Random Effects","State","State and Camp","State and Camp","State and Camp"),
                         c("Method","Random Intercept","Random Intercept","Uncorrelated Random Slope","Random Slope")), 
          covariate.labels = c('Distance to camp'))


# DV: RESENTMENT

ri.state.r <- lmer(resentment~gpc.Distance + gpc.prop_jewish25+gpc.unemployment33+gpc.population25+gpc.nazishare33 + 
                     ( 1 | state) ,evs,
                   control = lmerControl(optimizer ="Nelder_Mead"))
ri.state.camp.r <- lmer(resentment~gpc.Distance + gpc.prop_jewish25+gpc.unemployment33+gpc.population25+gpc.nazishare33 + 
                          ( 1 | state) + (1 | closest_camp),evs,
                        control = lmerControl(optimizer ="Nelder_Mead"))
rs.state.camp.r <- lmer(resentment~gpc.Distance + gpc.prop_jewish25+gpc.unemployment33+gpc.population25+gpc.nazishare33 + 
                          (1 + gpc.Distance| state) + (1 + gpc.Distance| closest_camp),evs,
                        control = lmerControl(optimizer ="Nelder_Mead"))
urs.state.camp.r <- lmer(resentment~gpc.Distance + gpc.prop_jewish25+gpc.unemployment33+gpc.population25+gpc.nazishare33 + 
                           (1 | state) + (0 + gpc.Distance | state) + 
                           (1 | closest_camp) + (0 + gpc.Distance | closest_camp),
                         evs, control = lmerControl(optimizer ="Nelder_Mead"))
anova(ri.state.r,ri.state.camp.r,rs.state.camp.r,urs.state.camp.r)
tab_model(ri.state.r,ri.state.camp.r,urs.state.camp.r,rs.state.camp.r,  digits = 3,
          show.se=TRUE, show.ci=FALSE, collapse.se=TRUE, show.p=FALSE, p.style="asterisk", p.threshold = c(0.05, 0.01))
stargazer(ri.state.r,ri.state.camp.r,urs.state.camp.r,rs.state.camp.r,dep.var.labels.include = FALSE,type="text",
          out="table4_panelb.txt", no.space=T, 
          keep = c("Distance"),
          star.cutoffs = c(0.05, 0.01, NA),  keep.stat = c("n","adj.rsq"),
          add.lines=list(c("Random Effects","State","State and Camp","State and Camp","State and Camp"),
                         c("Method","Random Intercept","Random Intercept","Uncorrelated Random Slope","Random Slope")), 
          covariate.labels = c('Distance to camp'))


# DV: FAR-RIGHT

ri.state.f <- lmer(far_right~gpc.Distance + gpc.prop_jewish25+gpc.unemployment33+gpc.population25+gpc.nazishare33 + 
                     ( 1 | state) ,evs,
                   control = lmerControl(optimizer ="Nelder_Mead"))
ri.state.camp.f <- lmer(far_right~gpc.Distance + gpc.prop_jewish25+gpc.unemployment33+gpc.population25+gpc.nazishare33 + 
                          ( 1 | state) + (1 | closest_camp),evs,
                        control = lmerControl(optimizer ="Nelder_Mead"))
rs.state.camp.f <- lmer(far_right~gpc.Distance + gpc.prop_jewish25+gpc.unemployment33+gpc.population25+gpc.nazishare33 + 
                          (1 + gpc.Distance| state) + (1 + gpc.Distance| closest_camp),evs,
                        control = lmerControl(optimizer ="Nelder_Mead"))
urs.state.camp.f <- lmer(far_right~gpc.Distance + gpc.prop_jewish25+gpc.unemployment33+gpc.population25+gpc.nazishare33 + 
                           (1 | state) + (0 + gpc.Distance | state) + 
                           (1 | closest_camp) + (0 + gpc.Distance | closest_camp),
                         evs, control = lmerControl(optimizer ="Nelder_Mead"))
anova(ri.state.f,ri.state.camp.f,rs.state.camp.f,urs.state.camp.f)
tab_model(ri.state.f,ri.state.camp.f,urs.state.camp.f,rs.state.camp.f, digits=3,
          show.se=TRUE, show.ci=FALSE, collapse.se=TRUE, show.p=FALSE, p.style="asterisk", p.threshold = c(0.05, 0.01))
stargazer(ri.state.f,ri.state.camp.f,urs.state.camp.f,rs.state.camp.f,dep.var.labels.include = FALSE,type="text",
          out="table4_panelc.txt", no.space=T, 
          keep = c("Distance"),
          star.cutoffs = c(0.05, 0.01, NA),  keep.stat = c("n","adj.rsq"),
          add.lines=list(c("Random Effects","State","State and Camp","State and Camp","State and Camp"),
                         c("Method","Random Intercept","Random Intercept","Uncorrelated Random Slope","Random Slope")), 
          covariate.labels = c('Distance to camp'))


####### FIGURE 2

# FIRST PLOT STATE SLOPES

# DV: INTOLERANCE
re1 <- ranef(rs.state.camp.i, condVar=TRUE, whichel = "state")
toplot <- as.data.frame(re1)
toplot.wide <- cbind(dcast(toplot, grp ~ term, value.var=c("condval")),dcast(toplot, grp ~ term, value.var=c("condsd")))
names(toplot.wide)[1] <- "state"
toplot.wide$grp <- NULL
names(toplot.wide)[2] <- "b_int"
names(toplot.wide)[3] <- "b_distance"
names(toplot.wide)[4] <- "se_int"
names(toplot.wide)[5] <- "se_distance"
p.s.i <- ggplot(toplot.wide,aes(y=b_distance,x=b_int)) +  geom_point(color="gray") + 
  geom_linerange(aes(ymin = b_distance - 2*se_distance, ymax = b_distance + 2*se_distance), color="gray") + 
  geom_text(data=subset(toplot.wide, (b_distance - 2*se_distance) > 0 | (b_distance + 2*se_distance) < 0), aes(label=state), 
            nudge_x = (max(toplot.wide$b_int) - min(toplot.wide$b_int))*.02) + 
  theme_classic() + coord_flip() + ggtitle("State Slopes: Intolerance") +
  geom_hline(yintercept = 0,linetype = "dashed") + xlab("Intercepts") + ylab("Effects of Distance")

# DV: RESENTMENT
re1 <- ranef(rs.state.camp.r, condVar=TRUE, whichel = "state")
toplot <- as.data.frame(re1)
toplot.wide <- cbind(dcast(toplot, grp ~ term, value.var=c("condval")),dcast(toplot, grp ~ term, value.var=c("condsd")))
names(toplot.wide)[1] <- "state"
toplot.wide$grp <- NULL
names(toplot.wide)[2] <- "b_int"
names(toplot.wide)[3] <- "b_distance"
names(toplot.wide)[4] <- "se_int"
names(toplot.wide)[5] <- "se_distance"
p.s.r <- ggplot(toplot.wide,aes(y=b_distance,x=b_int)) +  geom_point(color="gray") + 
  geom_linerange(aes(ymin = b_distance - 2*se_distance, ymax = b_distance + 2*se_distance), color="gray") + 
  geom_text(data=subset(toplot.wide, (b_distance - 2*se_distance) > 0 | (b_distance + 2*se_distance) < 0), aes(label=state), 
            nudge_x = (max(toplot.wide$b_int) - min(toplot.wide$b_int))*.02) + 
  theme_classic() + coord_flip() + ggtitle("State Slopes: Resentment") +
  geom_hline(yintercept = 0,linetype = "dashed") + xlab("Intercepts") + ylab("Effects of Distance")

# DV: FAR-RIGHT
re1 <- ranef(rs.state.camp.f, condVar=TRUE, whichel = "state")
toplot <- as.data.frame(re1)
toplot.wide <- cbind(dcast(toplot, grp ~ term, value.var=c("condval")),dcast(toplot, grp ~ term, value.var=c("condsd")))
names(toplot.wide)[1] <- "state"
toplot.wide$grp <- NULL
names(toplot.wide)[2] <- "b_int"
names(toplot.wide)[3] <- "b_distance"
names(toplot.wide)[4] <- "se_int"
names(toplot.wide)[5] <- "se_distance"
p.s.f <- ggplot(toplot.wide,aes(y=b_distance,x=b_int)) +  geom_point(color="gray") + 
  geom_linerange(aes(ymin = b_distance - 2*se_distance, ymax = b_distance + 2*se_distance), color="gray") + 
  geom_text(data=subset(toplot.wide, (b_distance - 2*se_distance) > 0 | (b_distance + 2*se_distance) < 0), aes(label=state), 
            nudge_x = (max(toplot.wide$b_int) - min(toplot.wide$b_int))*.02) + 
  theme_classic() + coord_flip() + ggtitle("State Slopes: Far-Right") +
  geom_hline(yintercept = 0,linetype = "dashed") + xlab("Intercepts") + ylab("Effects of Distance")


# NOW  PLOT THE CAMP SLOPES

# DV: INTOLERANCE
re1 <- ranef(rs.state.camp.i, condVar=TRUE, whichel = "closest_camp")
toplot <- as.data.frame(re1)
toplot.wide <- cbind(dcast(toplot, grp ~ term, value.var=c("condval")),dcast(toplot, grp ~ term, value.var=c("condsd")))
names(toplot.wide)[1] <- "closest_camp"
toplot.wide$grp <- NULL
names(toplot.wide)[2] <- "b_int"
names(toplot.wide)[3] <- "b_distance"
names(toplot.wide)[4] <- "se_int"
names(toplot.wide)[5] <- "se_distance"
p.c.i <- ggplot(toplot.wide,aes(y=b_distance,x=b_int)) +  geom_point(color="gray") + 
  geom_linerange(aes(ymin = b_distance - 2*se_distance, ymax = b_distance + 2*se_distance), color="gray") + 
  geom_text(data=subset(toplot.wide, (b_distance - 2*se_distance) > 0 | (b_distance + 2*se_distance) < 0), aes(label=closest_camp), 
            nudge_x = (max(toplot.wide$b_int) - min(toplot.wide$b_int))*.02) + 
  theme_classic() + coord_flip() + ggtitle("Camp Slopes: Intolerance") +
  geom_hline(yintercept = 0,linetype = "dashed") + xlab("Intercepts") + ylab("Effects of Distance")

# DV: RESENTMENT
re1 <- ranef(rs.state.camp.r, condVar=TRUE, whichel = "closest_camp")
toplot <- as.data.frame(re1)
toplot.wide <- cbind(dcast(toplot, grp ~ term, value.var=c("condval")),dcast(toplot, grp ~ term, value.var=c("condsd")))
names(toplot.wide)[1] <- "closest_camp"
toplot.wide$grp <- NULL
names(toplot.wide)[2] <- "b_int"
names(toplot.wide)[3] <- "b_distance"
names(toplot.wide)[4] <- "se_int"
names(toplot.wide)[5] <- "se_distance"
p.c.r <- ggplot(toplot.wide,aes(y=b_distance,x=b_int)) +  geom_point(color="gray") + 
  geom_linerange(aes(ymin = b_distance - 2*se_distance, ymax = b_distance + 2*se_distance), color="gray") + 
  geom_text(data=subset(toplot.wide, (b_distance - 2*se_distance) > 0 | (b_distance + 2*se_distance) < 0), aes(label=closest_camp), 
            nudge_x = (max(toplot.wide$b_int) - min(toplot.wide$b_int))*.02) + 
  theme_classic() + coord_flip() + ggtitle("Camp Slopes: Resentment") +
  geom_hline(yintercept = 0,linetype = "dashed")  + xlab("Intercepts") + ylab("Effects of Distance")

# DV: FAR-RIGHT
re1 <- ranef(rs.state.camp.f, condVar=TRUE, whichel = "closest_camp")
toplot <- as.data.frame(re1)
toplot.wide <- cbind(dcast(toplot, grp ~ term, value.var=c("condval")),dcast(toplot, grp ~ term, value.var=c("condsd")))
names(toplot.wide)[1] <- "closest_camp"
toplot.wide$grp <- NULL
names(toplot.wide)[2] <- "b_int"
names(toplot.wide)[3] <- "b_distance"
names(toplot.wide)[4] <- "se_int"
names(toplot.wide)[5] <- "se_distance"
p.c.f <- ggplot(toplot.wide,aes(y=b_distance,x=b_int)) +  geom_point(color="gray") + 
  geom_linerange(aes(ymin = b_distance - 2*se_distance, ymax = b_distance + 2*se_distance), color="gray") + 
  geom_text(data=subset(toplot.wide, (b_distance - 2*se_distance) > 0 | (b_distance + 2*se_distance) < 0), aes(label=closest_camp), 
            nudge_x = (max(toplot.wide$b_int) - min(toplot.wide$b_int))*.02) + 
  theme_classic() + coord_flip() + ggtitle("Camp Slopes: Far-Right") +
  geom_hline(yintercept = 0,linetype = "dashed") + xlab("Intercepts") + ylab("Effects of Distance")


# AND NOW COMBINE THEM
grid.arrange(p.s.i, p.s.r, p.s.f, 
             p.c.i, p.c.r, p.c.f, ncol=3)

jpeg("figure2.jpg", height=500, width=1000)
grid.arrange(p.s.i, p.s.r, p.s.f, 
             p.c.i, p.c.r, p.c.f, ncol=3)
dev.off()


############ TABLE 5: WEIMAR LANDER

# first read in the data
evs_weimar <- read.csv('evs_weimar.csv', stringsAsFactors = F)
evs_weimar$Distance <- evs_weimar$distance

# subset the data for prussia versus non-prussia
evs_weimar_noprussia <- evs_weimar[evs_weimar$oldland!=1000,]
evs_weimar_prussia <- evs_weimar[evs_weimar$oldland==1000,]


# estimate models

m1.olfe <- lm(intolerance~Distance+prop_jewish25+unemployment33+population25+nazishare33+factor(oldland) ,evs_weimar)
m2.olfe <- lm(resentment~Distance+prop_jewish25+unemployment33+population25+nazishare33+factor(oldland) ,evs_weimar)
m3.olfe <- lm(far_right~Distance+prop_jewish25+unemployment33+population25+nazishare33+factor(oldland) ,evs_weimar)
  
m1.olfe.np <- lm(intolerance~Distance+prop_jewish25+unemployment33+population25+nazishare33+factor(oldland) ,evs_weimar_noprussia)
m2.olfe.np <- lm(resentment~Distance+prop_jewish25+unemployment33+population25+nazishare33+factor(oldland) ,evs_weimar_noprussia)
m3.olfe.np <- lm(far_right~Distance+prop_jewish25+unemployment33+population25+nazishare33+factor(oldland) ,evs_weimar_noprussia)

m1.p <- lm(intolerance~Distance+prop_jewish25+unemployment33+population25+nazishare33,evs_weimar_prussia)
m2.p <- lm(resentment~Distance+prop_jewish25+unemployment33+population25+nazishare33,evs_weimar_prussia)
m3.p <- lm(far_right~Distance+prop_jewish25+unemployment33+population25+nazishare33,evs_weimar_prussia)

m1.p.fe <- lm(intolerance~Distance+prop_jewish25+unemployment33+population25+nazishare33+factor(oldland_prov) ,evs_weimar_prussia)
m2.p.fe <- lm(resentment~Distance+prop_jewish25+unemployment33+population25+nazishare33+factor(oldland_prov) ,evs_weimar_prussia)
m3.p.fe <- lm(far_right~Distance+prop_jewish25+unemployment33+population25+nazishare33+factor(oldland_prov) ,evs_weimar_prussia)

m1.plfe <- lm(intolerance~Distance+prop_jewish25+unemployment33+population25+nazishare33+factor(oldland_prov) ,evs_weimar)
m2.plfe <- lm(resentment~Distance+prop_jewish25+unemployment33+population25+nazishare33+factor(oldland_prov) ,evs_weimar)
m3.plfe <- lm(far_right~Distance+prop_jewish25+unemployment33+population25+nazishare33+factor(oldland_prov) ,evs_weimar)

# for hausman tests
form <- intolerance~Distance+prop_jewish25+unemployment33+population25+nazishare33
fe1 <- plm(form, data = evs_weimar_prussia, model = "within", index="oldland_prov")
po1 <- plm(form, data = evs_weimar_prussia, model = "pooling", index="oldland_prov")
fe.v.pooled1 <- round(phtest(fe1, po1)$p.value, digits=3)

form <- resentment~Distance+prop_jewish25+unemployment33+population25+nazishare33
fe2 <- plm(form, data = evs_weimar_prussia, model = "within", index="oldland_prov")
po2 <- plm(form, data = evs_weimar_prussia, model = "pooling", index="oldland_prov")
fe.v.pooled2 <- round(phtest(fe2, po2)$p.value, digits=3)

form <- far_right~Distance+prop_jewish25+unemployment33+population25+nazishare33
fe3 <- plm(form, data = evs_weimar_prussia, model = "within", index="oldland_prov")
po3 <- plm(form, data = evs_weimar_prussia, model = "pooling", index="oldland_prov")
fe.v.pooled3 <- round(phtest(fe3, po3)$p.value, digits=3)


# reweighting analyses
# first have to exclude provinces with no internal variation in Distance
data.evs_weimar_prussia <- data.frame(evs_weimar_prussia)
data.evs_weimar_prussia <- data.evs_weimar_prussia[data.evs_weimar_prussia$oldland_pruprov!=1100 & data.evs_weimar_prussia$oldland_pruprov!=2200,]

m1.p.iwe <- EstimateIWE(y="intolerance", treatment="Distance", group="oldland_prov", 
                      controls=c("prop_jewish25","unemployment33","population25","nazishare33"),
                      data=data.evs_weimar_prussia, subset=NULL, cluster.var=NULL,is.robust=TRUE)
m1.iwe <- rbind(m1.p.iwe$swe.est, sqrt(m1.p.iwe$swe.var), m1.p.iwe$swe.est/sqrt(m1.p.iwe$swe.var))
m1.p.rwe <- EstimateRWE(y="intolerance", treatment="Distance", group="oldland_prov", 
                        controls=c("prop_jewish25","unemployment33","population25","nazishare33"),
                        data=data.evs_weimar_prussia, subset=NULL, cluster.var=NULL,is.robust=TRUE)
m1.rwe <- rbind(m1.p.rwe$swe.est, sqrt(m1.p.rwe$swe.var), m1.p.rwe$swe.est/sqrt(m1.p.rwe$swe.var))

m2.p.iwe <- EstimateIWE(y="resentment", treatment="Distance", group="oldland_prov", 
                        controls=c("prop_jewish25","unemployment33","population25","nazishare33"),
                        data=data.evs_weimar_prussia, subset=NULL, cluster.var=NULL,is.robust=TRUE)
m2.iwe <- rbind(m2.p.iwe$swe.est, sqrt(m2.p.iwe$swe.var), m2.p.iwe$swe.est/sqrt(m2.p.iwe$swe.var))
m2.p.rwe <- EstimateRWE(y="resentment", treatment="Distance", group="oldland_prov", 
                        controls=c("prop_jewish25","unemployment33","population25","nazishare33"),
                        data=data.evs_weimar_prussia, subset=NULL, cluster.var=NULL,is.robust=TRUE)
m2.rwe <- rbind(m2.p.rwe$swe.est, sqrt(m2.p.rwe$swe.var), m2.p.rwe$swe.est/sqrt(m2.p.rwe$swe.var))

m3.p.iwe <- EstimateIWE(y="far_right", treatment="Distance", group="oldland_prov", 
                        controls=c("prop_jewish25","unemployment33","population25","nazishare33"),
                        data=data.evs_weimar_prussia, subset=NULL, cluster.var=NULL,is.robust=TRUE)
m3.iwe <- rbind(m3.p.iwe$swe.est, sqrt(m3.p.iwe$swe.var), m3.p.iwe$swe.est/sqrt(m3.p.iwe$swe.var))
m3.p.rwe <- EstimateRWE(y="far_right", treatment="Distance", group="oldland_prov", 
                        controls=c("prop_jewish25","unemployment33","population25","nazishare33"),
                        data=data.evs_weimar_prussia, subset=NULL, cluster.var=NULL,is.robust=TRUE)
m3.rwe <- rbind(m3.p.rwe$swe.est, sqrt(m3.p.rwe$swe.var), m3.p.rwe$swe.est/sqrt(m3.p.rwe$swe.var))

results <- round(cbind(m1.iwe, m1.rwe,m2.iwe, m2.rwe,m3.iwe, m3.rwe),3)
colnames(results) <- c("Intolerance IWE","Intolerance RWE","Resentment IWE","Resentment RWE","Far-right IWE","Far-right RWE")
write.table(results, file="table5_columns5and6.txt", row.names=FALSE)



tab_model(m1.olfe, m1.olfe.np, m1.p, m1.p.fe, m1.plfe, digits=3,
          show.se=TRUE, show.ci=FALSE, collapse.se=TRUE, show.p=FALSE, p.style="asterisk", p.threshold = c(0.05, 0.01))
tab_model(m2.olfe, m2.olfe.np, m2.p, m2.p.fe, m2.plfe, digits=3,
          show.se=TRUE, show.ci=FALSE, collapse.se=TRUE, show.p=FALSE, p.style="asterisk", p.threshold = c(0.05, 0.01))
tab_model(m3.olfe, m3.olfe.np, m3.p, m3.p.fe, m3.plfe, digits=3,
          show.se=TRUE, show.ci=FALSE, collapse.se=TRUE, show.p=FALSE, p.style="asterisk", p.threshold = c(0.05, 0.01))

stargazer(m1.olfe, m1.olfe.np, m1.p, m1.p.fe, m1.plfe, dep.var.labels.include = FALSE,type="text",
          out="table5_panela.txt", no.space=T, 
          keep = c("Distance"),
          star.cutoffs = c(0.05, 0.01, NA),  keep.stat = c("n","adj.rsq"),
          add.lines=list(c("Sample","All","Not Prussia","Prussia","Prussia","All"),
                         c("Fixed Effects","Weimar States","Weimar States","None","Prussian Provinces","Weimar States and Prussian Provinces"),
                         c("Hausman","","","",fe.v.pooled1,"")), 
          covariate.labels = c('Distance to camp'))

stargazer(m2.olfe, m2.olfe.np, m2.p, m2.p.fe, m2.plfe, dep.var.labels.include = FALSE,type="text",
          out="table5_panelb.txt", no.space=T, 
          keep = c("Distance"),
          star.cutoffs = c(0.05, 0.01, NA),  keep.stat = c("n","adj.rsq"),
          add.lines=list(c("Sample","All","Not Prussia","Prussia","Prussia","All"),
                         c("Fixed Effects","Weimar States","Weimar States","None","Prussian Provinces","Weimar States and Prussian Provinces"),
                         c("Hausman","","","",fe.v.pooled2,"")), 
          covariate.labels = c('Distance to camp'))

stargazer(m3.olfe, m3.olfe.np, m3.p, m3.p.fe, m3.plfe, dep.var.labels.include = FALSE,type="text",
          out="table5_panelc.txt", no.space=T, 
          keep = c("Distance"),
          star.cutoffs = c(0.05, 0.01, NA),  keep.stat = c("n","adj.rsq"),
          add.lines=list(c("Sample","All","Not Prussia","Prussia","Prussia","All"),
                         c("Fixed Effects","Weimar States","Weimar States","None","Prussian Provinces","Weimar States and Prussian Provinces"),
                         c("Hausman","","","",fe.v.pooled3,"")), 
          covariate.labels = c('Distance to camp'))